package org.abettor.pushbox.upload;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.util.Log;

/**
 * 检查上传返回的
 * @author jzh
 *
 */
public class ParaUploadRetValue {
	public static final int PUSH_BOX_UPLOAD_TYPE_NETUSERMAP = 0;//普通网友地图库
	public static final int PUSH_BOX_UPLOAD_TYPE_QUESSIONMAP = 1;//网友求解地图
	public static final int NICKNAME_EXITS = 1;//昵称已经存在
	public static final int NICKNAME_NOT_EXITS = 0;//昵称没有存在
	public static final int NET_RET_ERROR = -1;//网络返回的值
	public static final int UPLOAD_NICK_SUCCESS = 1;//更新昵称成功
	public static final int UPLOAD_NICK_FAILED = 0;//更新昵称失败
	public static final int UPLOAD_NICK_EXITS = 2;//昵称已经存在
	public static final int LOGIN_SUCCESS = 1;//登录成功
	public static final int LONGIN_FAIL = 0;//登录失败
	public static final int UPDATEPASS_SUCCESS = 1;//修改密码成功
	public static final int UPDATEPASS_FAILED = 0;//修改密码失败
	public static final int UPLOAD_MAP_RET_SUCCESS = 1;//上传文件成功
	public static final int UPLOAD_MAP_RET_MAPHASEXIST = 2;//地图已经存在了
	public static final int UPLOAD_MAP_RET_OTHERERROR = 3;//其他错误
	public static final int CHANGE_NET_MAP_STATE_SUCCESS = 1;//改变网络地图的状态成功
	public static final int SEND_REPLY_MSG_SUCCESS = 1;//更新反馈消息是否成功
	public static final int ANSWER_UPLOAD_SUCCESS = 1;//答案上传成功
	public static final int SEND_COMMENTS_SUCCESS = 1;//上传评论成功
	public static final int SEND_COMMENTS_FAIL = 0;//上传评论失败
	/**
	 * 上传地图的返回值
	 * @author jzh
	 *
	 */
	public static class UploadMapValue{
		private int key;//返回的值
		private int id;//地图的id
		public int getKey() {
			return key;
		}
		public void setKey(int key) {
			this.key = key;
		}
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
	}
	/**
	 * 解析收到的值
	 * @param str
	 * @return
	 */
	public static UploadMapValue paraRetValue(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			int retValue = Integer.parseInt(value);
			if(retValue==UPLOAD_MAP_RET_SUCCESS){//
				UploadMapValue ret = new UploadMapValue();
				ret.setKey(retValue);
				NodeList l = doc.getElementsByTagName("i");
				Node n = l.item(0);
				String v = n.getFirstChild().getNodeValue();
				int ii = Integer.parseInt(v);
				ret.setId(ii);
				return ret;
			}else{
				UploadMapValue ret = new UploadMapValue();
				ret.setKey(retValue);
				return ret;
			}
		}catch(Exception e){
			UploadMapValue ret = new UploadMapValue();
			ret.setKey(UPLOAD_MAP_RET_OTHERERROR);
			return ret;
		}finally{
			try{
				in.close();
			}catch(Exception e){
			}
		}
	}
	/**
	 * 上传答案是否成功
	 * @param content
	 * @return
	 */
	public static boolean isAnswerUploadSuccess(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			int retValue = Integer.parseInt(value);
			if(retValue==ANSWER_UPLOAD_SUCCESS){
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			throw new IllegalStateException();
		}finally{
			try {
				in.close();
			} catch (Exception e) {
			}
		}
	}
	/**
	 * 更新网络地图是否成功
	 * @param content
	 * @return
	 */
	public static boolean isUpadateNetStateSuccess(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			int retValue = Integer.parseInt(value);
			if(retValue==CHANGE_NET_MAP_STATE_SUCCESS){
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			throw new IllegalStateException();
		}finally{
			try {
				in.close();
			} catch (Exception e) {
			}
		}
	}
	/**
	 * 更新消息是否成功
	 * @param content
	 * @return
	 */
	public static boolean isSendRplyMsgSuccess(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			int retValue = Integer.parseInt(value);
			if(retValue==SEND_REPLY_MSG_SUCCESS){
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			throw new IllegalStateException();
		}finally{
			try {
				in.close();
			} catch (Exception e) {
			}
		}
	}
	/**
	 * 查询昵称是否存在
	 * @param content
	 * @return
	 */
	public static boolean isNickNameExits(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			int retValue = Integer.parseInt(value);
			if(retValue==NICKNAME_EXITS){
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			throw new IllegalStateException();
		}finally{
			try {
				in.close();
			} catch (Exception e) {
			}
		}
	}
	/**
	 * 更新昵称是否成功
	 * @param content
	 * @return
	 */
	public static int isUploadNickSuccess(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			int retValue = Integer.parseInt(value);
			return retValue;
		}catch(Exception e){
			throw new IllegalStateException();
		}finally{
			try {
				in.close();
			} catch (IOException e) {
			}
		}
	}
	/**
	 * 送鲜花的操作
	 * @param content
	 * @return
	 */
	public static int paraSendFlowerCount(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			int retValue = Integer.parseInt(value);
			return retValue;
		}catch(Exception e){
			return 0;
		}finally{
			try {
				in.close();
			} catch (IOException e) {
			}
		}
	}
	/**
	 * 扔臭鸡蛋操作
	 * @param countent
	 * @return
	 */
	public static int paraSendEggCount(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			int retValue = Integer.parseInt(value);
			return retValue;
		}catch(Exception e){
			return 0;
		}finally{
			try {
				in.close();
			} catch (IOException e) {
			}
		}
	}
	/**
	 * 登录是否成功
	 * @param content
	 * @return
	 */
	public static boolean isLoginNickSuccess(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			int retValue = Integer.parseInt(value);
			return (retValue==LOGIN_SUCCESS);
		}catch(Exception e){
			throw new IllegalStateException();
		}finally{
			try {
				in.close();
			} catch (IOException e) {
			}
		}
	}
	/**
	 * 添加评论的操作
	 * @param content
	 * @return
	 */
	public static boolean isSendCommentsSuccess(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			int retValue = Integer.parseInt(value);
			return (retValue==SEND_COMMENTS_SUCCESS);
		}catch(Exception e){
			Log.e("error", "error", e);
			return false;
		}finally{
			try {
				in.close();
			} catch (IOException e) {
			}
		}
	}
	/**
	 * 修改密码是否成功
	 * @param content
	 * @return
	 */
	public static boolean isUpdatePassSuccess(String content){
		ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes());
		try{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(in);
			NodeList list = doc.getElementsByTagName("v");
			Node node = list.item(0);
			String value = node.getFirstChild().getNodeValue();
			int retValue = Integer.parseInt(value);
			return (retValue==UPDATEPASS_SUCCESS);
		}catch(Exception e){
			throw new IllegalStateException();
		}finally{
			try {
				in.close();
			} catch (IOException e) {
			}
		}
	}
}
